package com.gexiaobao.pigeon.ui.fragment.mine.tools.query

import android.os.Bundle
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.databinding.FragmentFindPigeonBinding
import com.gexiaobao.pigeon.viewmodel.MineToolsViwModel
import com.google.android.material.tabs.TabLayout
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat


/**
 * @Author      : hxw
 * @Date        : 2023/8/3 9:34
 * @Describe    : 查赛鸽
 */
class FragmentFindPigeon : BaseFragment<MineToolsViwModel, FragmentFindPigeonBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.toolBarFind.toolbar.initClose("查赛鸽") {
            nav().navigateUp()
        }
        initTabLayout()
    }


    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.llHistoryData) {
            when (it) {
                mDatabind.llHistoryData -> {
                    nav().navigateAction(R.id.action_to_fragment_find_history)
                }
            }
        }
    }

    private fun initTabLayout() {
        val tab1 = mDatabind.tabLayout.newTab().setText("足环搜索")
        mDatabind.tabLayout.addTab(tab1)
        val tab2 = mDatabind.tabLayout.newTab().setText("鸽主搜索")
        mDatabind.tabLayout.addTab(tab2)

        mDatabind.tabLayout.addOnTabSelectedListener(object : TabLayout.BaseOnTabSelectedListener<TabLayout.Tab> {
            override fun onTabReselected(p0: TabLayout.Tab) {}
            override fun onTabUnselected(p0: TabLayout.Tab) {}

            override fun onTabSelected(p0: TabLayout.Tab) {
                replaceFragments(p0.position)
            }
        })
        replaceFragments(0)
    }

    private fun replaceFragments(position: Int) {
        childFragmentManager.beginTransaction().apply {
            when (position) {
                0 -> replace(R.id.FindPigeonFragContainer, FragmentSearchPigeon.newInstance(1))
                1 -> replace(R.id.FindPigeonFragContainer, FragmentSearchPigeon.newInstance(2))
            }
        }.commitAllowingStateLoss()
    }
}